﻿using AwesomeChromeFormUI.Cache;
using AwesomeChromeFormUI.Constants;
using AwesomeChromeFormUI.Dialogs;
using AwesomeChromeFormUI.Entity;
using AwesomeChromeFormUI.Native;
using AwesomeChromeFormUI.Utils;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AwesomeChromeFormUI.CommonExtensions
{
    public static class FormExtensions
    {
        /// <summary>
        /// 无边框窗口拖拽
        /// SC_MOVE + HTCAPTION 是将移动窗口的命令与标题栏的点击组合起来，以便在拖动标题栏时移动窗口
        /// 当用户在当前窗口按住鼠标左键并拖动时，鼠标位置会被识别为位于标题栏上，从而触发移动窗口的操作
        /// </summary>
        public static void DragWindow(this Form form)
        {
            form.InvokeOnUiThreadIfRequired(() => {
                User32ApiNativeMethods.ReleaseCapture();
                User32ApiNativeMethods.SendMessage(form.Handle, User32ApiNativeMethods.WM_SYSCOMMAND, User32ApiNativeMethods.SC_MOVE + User32ApiNativeMethods.HTCAPTION, 0);
            });
        }


        /// <summary>
        /// 处理窗口状态：最大化/正常化/最小化/关闭
        /// </summary>
        /// <param name="type"></param>
        public static void ChangeWindowState(this Form form,string type)
        {
            form.InvokeOnUiThreadIfRequired(() => {

                if (type.Equals(SystemConstant.MINIMIZED))
                {
                    form.WindowState = FormWindowState.Minimized;
                    return;
                }
                if (type.Equals(SystemConstant.MAXIMIZED))
                {
                    form.WindowState = FormWindowState.Maximized;
                    return;
                }
                if (type.Equals(SystemConstant.NORMALIZED))
                {
                    string key = form.Handle.ToString();
                    FormLocationEntity formLocationEntity = GlobalFormHandleCache.Cache.GetValueByKey(key);

                    if(formLocationEntity == null)
                    {
                        return;
                    }

                    if (!formLocationEntity.isMaximized)
                    {
                        // 更新缓存
                        formLocationEntity.lastWidth = form.Width;
                        formLocationEntity.lastHeight = form.Height;
                        formLocationEntity.lastLocation = form.Location;
                        formLocationEntity.isMaximized = true;

                        GlobalFormHandleCache.Cache.Update(key, formLocationEntity);

                        // 调整窗口宽高
                        form.Height = Screen.PrimaryScreen.WorkingArea.Height;
                        form.Width = Screen.PrimaryScreen.WorkingArea.Width;
                        form.Location = new Point(0,0);

                        // 设置窗口区域为圆角
                        form.Region = new Region(Drawing2DUtil.GetRoundedRect(new Rectangle(0, 0, form.Width, form.Height), 20));

                        // 任务栏高度
                        //int taskbarHeight = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
                        return;
                    }

                    // 调整窗口宽高
                    form.Height = formLocationEntity.lastHeight;
                    form.Width = formLocationEntity.lastWidth;
                    form.Location = formLocationEntity.lastLocation;

                    // 设置窗口区域为圆角
                    form.Region = new Region(Drawing2DUtil.GetRoundedRect(new Rectangle(0, 0, form.Width, form.Height), 20));

                    // 更新缓存
                    formLocationEntity.isMaximized = false;
                    GlobalFormHandleCache.Cache.Update(key, formLocationEntity);

                    return;
                }
                if (type.Equals(SystemConstant.CLOSE))
                {
                    form.Close();
                    return;
                }
            });
        }

        /// <summary>
        /// 获取文件夹路径
        /// </summary>
        /// <param name="form"></param>
        /// <param name="useDefaultDialog"></param>
        /// <returns></returns>
        public static Task<string> GetSelectedFolderPath(this Form form,bool useDefaultDialog=false)
        {
            // 使用TaskCompletionSource来创建一个未完成的任务
            TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();

            // 在 UI 线程上执行操作
            form.InvokeOnUiThreadIfRequired(() =>
            {
                // 执行回调方法，并获取结果
                string result = useDefaultDialog? DefaultFolderBrowserDialog.ShowFolderBrowserDialog():DefaultFolderBrowserDialog.ShowVistaFolderBrowserDialog();
                // 将结果设置到任务完成源，并标记任务为成功
                tcs.SetResult(result);
            });

            // 返回任务对象
            return tcs.Task;
        }

    }
}
